package com.itheima.sfbx.insurance.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.itheima.sfbx.framework.commons.basic.ResponseResult;
import com.itheima.sfbx.framework.commons.utils.ResponseResultBuild;
import com.itheima.sfbx.insurance.dto.SafeguardVO;
import com.itheima.sfbx.insurance.service.ISafeguardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "保障項管理")
@RestController
@RequestMapping("/safeguard")
public class SafeguardController {

    @Autowired
    private ISafeguardService safeguardService;

    /**
     * 查询保障项列表
     * @param safeguardVO 查询条件
     * @param pageNum 页号
     * @param pageSize 页大小
     * @return 返回结果
     */
    @ApiOperation(value = "根据条件分页查询保障性",notes = "根据条件分页查询保障项")
    //指定swagger的方法参数有哪些，以后在swagger的文档上看得到这些形参的具体属性
    @ApiImplicitParams({
            @ApiImplicitParam(name = "SafeguardVO",value = "查询条件",required = true,dataType = "SafeguardVO"),
            @ApiImplicitParam(name = "pageNum",value = "页号",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize",value = "页大小",required = true,dataType = "Integer")
    })
    //在查询条件对象中指定哪些属性可以展示在swagger中
    @ApiOperationSupport(includeParameters = {"SafeguardVO.checkedType","SafeguardVO.checkedKey","SafeguardVO.checkedKeyName"})
    @PostMapping("/page/{pageNum}/{pageSize}")
    public ResponseResult<Page<SafeguardVO>> findSafeguardVoPage(@RequestBody SafeguardVO safeguardVO,
                                                                 @PathVariable("pageNum") Integer pageNum,
                                                                 @PathVariable("pageSize") Integer pageSize
                                                                ){
        Page<SafeguardVO> page = safeguardService.findPage(safeguardVO, pageNum, pageSize);
        return ResponseResultBuild.successBuild(page);
    }

    /**
     * 新增保障项
     * @param safeguardVO 保障项信息
     * @return 保障项信息
     */
    @ApiOperation(value = "新增保障项",notes = "新增保障项")
    @ApiImplicitParam(name = "SafeguardVO",value = "保障项信息",required = true,dataType = "SafeguardVO")
    @ApiOperationSupport(includeParameters = {"SafeguardVO.checkedType","SafeguardVO.checkedKey","SafeguardVO.checkedKeyName"})
    @PutMapping
    public ResponseResult<SafeguardVO> createSafeguard(@RequestBody SafeguardVO safeguardVO){
        SafeguardVO result = safeguardService.save(safeguardVO);
        return ResponseResultBuild.successBuild(result);
    }

    /**
     * 修改保障项
     * @param safeguardVO 保障项信息
     * @return 修改后的保障项信息
     */
    @ApiOperation(value = "修改保障项",notes = "修改保障项")
    @ApiImplicitParam(name = "SafeguardVO",value = "保障项信息",required = true,dataType = "SafeguardVO")
    @ApiOperationSupport(includeParameters = {"SafeguardVO.checkedType","SafeguardVO.checkedKey","SafeguardVO.checkedKeyName"})
    @PatchMapping
    public ResponseResult<Boolean> updateSafeguard(@RequestBody SafeguardVO safeguardVO){
        Boolean result = safeguardService.update(safeguardVO);
        return ResponseResultBuild.successBuild(result);
    }

    /**
     * 删除保障项
     * @param safeguardVO 删除的保障项
     * @return 删除结果
     */
    @ApiOperation(value = "删除保障项",notes = "删除保障项")
    @ApiImplicitParam(name = "safeguardVO",value = "保障项对象",required = true,dataType = "SafeguardVO")
    @ApiOperationSupport(includeParameters = {"SafeguardVO.checkedType","SafeguardVO.checkedKey","SafeguardVO.checkedKeyName"})
    @DeleteMapping
    public ResponseResult<Boolean> deleteSafeguard(@RequestBody SafeguardVO safeguardVO){
        Boolean result = safeguardService.delete(safeguardVO.getCheckedIds());
        return ResponseResultBuild.successBuild(result);
    }
}
